'use strict';

module.exports = app => {
    return class GuardianService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 根据监护人的编号获取孩子列表
         * @param id
         * @returns {Promise<void>}
         */
        async getChildren() {
            let url = this.app.urls('getChildren');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 根据监护人的编号获取监护人信息我的里面
         * type: 类型 1. 教师 2. 家长 3.学生
         * @param id
         * @returns {Promise<void>}
         */
        async getGuardianInfo({ id, type}) {
            let url = this.app.urls('getGuardianInfo');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    type
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 根据监护人的编号获取监护人信息
         * @param id
         * @returns {Promise<void>}
         */
        async getGuardian({ id }) {
            let url = this.app.urls('getGuardian');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 监护人列表
         * @param studentId
         * @returns {Promise<void>}
         */
        async list() {
            let url = this.app.urls('guardianList');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {}
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 添加监护人
         * @param params
         * @returns {Promise<void>}
         */
        async edit(params) {
            let url = this.app.urls('guardianAdd');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 删除监护人
         * @param id
         * @returns {Promise<void>}
         */
        async delete({id}) {
            let url = this.app.urls('guardianDelete');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 修改电话号码
         * @param mobile
         * @returns {Promise<void>}
         */
        async updateMobile({mobile}) {
            let url = this.app.urls('updateMobile');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    mobile
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


    }

};
